package com.pzhu.gmall.search.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import com.pzhu.gmall.bean.PmsBaseAttrInfo;
import com.pzhu.gmall.bean.PmsBaseAttrValue;
import com.pzhu.gmall.dto.PmsSearchCrumb;
import com.pzhu.gmall.dto.PmsSearchParmDTO;
import com.pzhu.gmall.dto.PmsSearchSkuInfoDTO;
import com.pzhu.gmall.service.SearchService;
import com.pzhu.gmall.web.annotations.LoginRequired;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author 刘鹏 liupeng
 * @date 2019/11/12
 **/
@Controller
@CrossOrigin
public class SkuSearchController {

    @Reference
    private SearchService searchService;

    @RequestMapping("list.html")
    public String list(PmsSearchParmDTO pmsSearchParmDTO, ModelMap modelMap) {
        String keyword = pmsSearchParmDTO.getKeyword();
        String catalog3Id = pmsSearchParmDTO.getCatalog3Id();
        String[] valueIds = pmsSearchParmDTO.getValueIds();
        PmsSearchSkuInfoDTO search = searchService.search(pmsSearchParmDTO);
        modelMap.put("skuLsInfoList", search.getSearchSkuInfoList());
        List<PmsSearchCrumb> pmsSearchCrumbList = new ArrayList<>(valueIds == null ? 0 : valueIds.length);
        if (valueIds != null) {
            List<PmsBaseAttrInfo> pmsBaseAttrInfoList = search.getPmsBaseAttrInfoList();
            Iterator<PmsBaseAttrInfo> iterator = pmsBaseAttrInfoList.iterator();
            while (iterator.hasNext()) {
                PmsBaseAttrInfo pmsBaseAttrInfo = iterator.next();
                List<PmsBaseAttrValue> attrValueList = pmsBaseAttrInfo.getAttrValueList();
                for (PmsBaseAttrValue pmsBaseAttrValue : attrValueList) {
                    List<String> valueIdList = Arrays.asList(valueIds);
                    if (valueIdList.contains(pmsBaseAttrValue.getId())) {
                        PmsSearchCrumb pmsSearchCrumb = new PmsSearchCrumb();
                        pmsSearchCrumb.setValueId(pmsBaseAttrValue.getId());
                        pmsSearchCrumb.setValueName(pmsBaseAttrValue.getValueName());
                        pmsSearchCrumb.setUrlParam(getUrlParamForCrumb(pmsSearchParmDTO, pmsBaseAttrValue.getId()));
                        pmsSearchCrumbList.add(pmsSearchCrumb);
                        iterator.remove();
                    }
                }
            }
        }
        modelMap.put("attrValueSelectedList", pmsSearchCrumbList);

        modelMap.put("attrList", search.getPmsBaseAttrInfoList());
        StringBuilder urlParam = new StringBuilder();
        if (StringUtils.isNotBlank(keyword)) {
            urlParam.append("&keyword=" + keyword);
        }
        if (StringUtils.isNotBlank(catalog3Id)) {
            urlParam.append("&catalog3Id=" + catalog3Id);
        }
        if (valueIds != null) {
            Arrays.asList(valueIds).forEach(valueId -> {
                urlParam.append("&valueIds=" + valueId);
            });
        }
        modelMap.put("urlParam", trimUrl(urlParam));
        return "list";
    }

    private String getUrlParamForCrumb(PmsSearchParmDTO pmsSearchParmDTO, String delValueId) {
        String keyword = pmsSearchParmDTO.getKeyword();
        String catalog3Id = pmsSearchParmDTO.getCatalog3Id();
        String[] skuAttrValueList = pmsSearchParmDTO.getValueIds();

        String urlParam = "";

        if (StringUtils.isNotBlank(keyword)) {
            if (StringUtils.isNotBlank(urlParam)) {
                urlParam = urlParam + "&";
            }
            urlParam = urlParam + "keyword=" + keyword;
        }

        if (StringUtils.isNotBlank(catalog3Id)) {
            if (StringUtils.isNotBlank(urlParam)) {
                urlParam = urlParam + "&";
            }
            urlParam = urlParam + "catalog3Id=" + catalog3Id;
        }

        if (skuAttrValueList != null) {
            for (String pmsSkuAttrValue : skuAttrValueList) {
                if (!pmsSkuAttrValue.equals(delValueId)) {
                    urlParam = urlParam + "&valueIds=" + pmsSkuAttrValue;
                }
            }
        }

        return urlParam;
    }

    private String trimUrl(StringBuilder urlParam) {
        char c = urlParam.charAt(0);
        String urlPramStr = "";
        if (c == '&') {
            urlPramStr = urlParam.substring(1);
        } else {
            urlPramStr = urlParam.toString();
        }
        return urlPramStr;
    }

    @RequestMapping("/index")
    @LoginRequired(needSuccess = false)
    public String index() {
        return "index";
    }

}
